﻿#region License

/*
 © 2012 Alex Kazaev
 * FolderDrive project [http://folderdrive.codeplex.com/]
 * Licensed under Ms-PL license [http://www.opensource.org/licenses/MS-PL]
 */

#endregion License

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows;

namespace FolderDrive.ViewModels
{
  /// <summary>
  /// View Model class for add mapping control
  /// </summary>
  public class AddMappingViewModel : BaseViewModel
  {
    #region fields

    private List<string> availableDriveLetters = new List<string>();
    private Visibility errorNotificationVisibility;
    private string selectedDriveLetter;
    private string selectedPath;
    private bool isPermanent;

    #endregion fields

    #region properties

    public MappingPreferenceItem MappingPreference { get; private set; }

    public List<string> AvailableDriveLetters
    {
      get { return availableDriveLetters; }
      set
      {
        availableDriveLetters = value;
        NotifyChanged("AvailableDriveLetters");
      }
    }

    public string SelectedDriveLetter
    {
      get { return selectedDriveLetter; }
      set
      {
        selectedDriveLetter = value;
        NotifyChanged("SelectedDriveLetter");
      }
    }

    public string SelectedPath
    {
      get { return selectedPath; }
      set
      {
        selectedPath = value;
        NotifyChanged("SelectedPath");
      }
    }

    public bool IsPermanent
    {
      get { return isPermanent; }
      set { isPermanent = value; NotifyChanged("IsPermanent"); }
    }

    public Visibility ErrorNotificationVisibility
    {
      get { return errorNotificationVisibility; }
      set
      {
        errorNotificationVisibility = value;
        NotifyChanged("ErrorNotificationVisibility");
      }
    }

    #endregion properties

    #region constructor

    /// <summary>
    /// Initializes a new instance of the <see cref="AddMappingViewModel"/> class.
    /// </summary>
    public AddMappingViewModel()
    {
      AvailableDriveLetters = GetAvailableDriveLetters();
      SelectedDriveLetter = AvailableDriveLetters.First();
      ErrorNotificationVisibility = Visibility.Hidden;
    }

    #endregion constructor

    #region methods

    /// <summary>
    /// Retrieves all available drive letters
    /// </summary>
    /// <returns></returns>
    private static List<string> GetAvailableDriveLetters()
    {
      var letters = new List<string>();
      DriveInfo[] drives = DriveInfo.GetDrives();

      //first let's get all avilable drive letters
      for (int i = Convert.ToInt16('A'); i <= Convert.ToInt16('Z'); i++)
      {
        string driveLetter = ((char)i) + ":";
        if (!drives.Any(drive => drive.Name.StartsWith(driveLetter)))
        {
          letters.Add(driveLetter);
        }
      }
      //return the letters left
      return letters;
    }

    /// <summary>
    /// Checks the data and generates mapping item.
    /// </summary>
    /// <returns></returns>
    public bool CheckDataAndGenerateMappingItem()
    {
      bool result = SelectedDriveLetter != null && Directory.Exists(SelectedPath);
      if (result)
      {
        MappingPreference = new MappingPreferenceItem(SelectedDriveLetter, SelectedPath, IsPermanent);
      }
      else
      {
        ErrorNotificationVisibility = Visibility.Visible;
      }
      return result;
    }

    #endregion methods
  }
}